package com.controller;

import com.entity.Role;
import com.entity.Storage;
import com.entity.User;
import com.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @RequestMapping("/list")
    public ModelAndView finRole(){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> list = roleService.finRole();
        modelAndView.addObject("roleList",list);
        modelAndView.setViewName("role/list");
        return modelAndView;
    }

    @RequestMapping("/view")
    public String roleId(@RequestParam(required = true)int id, Model model,String name){
        Role role = roleService.roleId(id);
        model.addAttribute("role",role);
        if (name.equals("查看")) {
           return "role/view";
        }else {
            return "role/update";
        }
    }

    @RequestMapping("/update")
    public String updateRole(Role role){
        int update = roleService.updateRole(role);
        if (update>0){
            return "frame";
        }else {
            return "role/update";
        }

    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "role/add";
    }

    @RequestMapping("/add")
    public String add(Role role){
        int add = roleService.insertRole(role);
        if (add>0){
            return "frame";
        }else {
            return "role/update";
        }
    }

    @RequestMapping("/wher")
    public ModelAndView wher(String queryRoleCode,String queryRoleName){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> list = roleService.listRole(queryRoleCode, queryRoleName);
        modelAndView.addObject("roleList",list);
        modelAndView.setViewName("role/list");
        return modelAndView;
    }
}
